package com.exam.springbootexam.controller;
import com.exam.springbootexam.entity.PageEntity;
import com.exam.springbootexam.entity.Student;
import com.exam.springbootexam.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author 十五
 */
@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 分页查询学生信息
     *
     * @param current  当前页码
     * @param pageSize 每页显示数量
     * @return 包含分页学生信息的响应实体
     */
    @GetMapping("/{current}/{pageSize}")
    public ResponseEntity<PageEntity<Student>> page(@PathVariable(value = "current") Integer current, @PathVariable(value = "pageSize") Integer pageSize) {
        PageEntity<Student> studentPageEntity = studentService.selectByPage(current, pageSize);
        return ResponseEntity.ok(studentPageEntity);
    }

    /**
     * 根据学生ID查询学生信息
     *
     * @param id 学生ID
     * @return 查询到的学生信息或错误响应实体
     */
    @GetMapping("/{id}")
    public ResponseEntity<Student> findById(@PathVariable(value = "id") Integer id) {
        Optional<Student> student = Optional.ofNullable(studentService.selectById(id));
        return student.map(ResponseEntity::ok).orElse(ResponseEntity.badRequest().build());
    }

    /**
     * 查询所有学生信息
     *
     * @return 所有学生信息列表的响应实体
     */
    @GetMapping("/all")
    public ResponseEntity<List<Student>> findAll() {
        List<Student> students = studentService.selectAll();
        return ResponseEntity.ok(students);
    }

    /**
     * search 搜索
     */
    @GetMapping("/search")
    public ResponseEntity<List<Student>> search(@RequestParam String keyword) {
        List<Student> students = studentService.serachByKeyword(keyword);
        return ResponseEntity.ok(students);
    }

    /**
     * 修改学生信息
     *
     * @param student 学生对象
     * @return 更新结果的响应实体
     */
    @PostMapping("/modify")
    public ResponseEntity<?> update(@RequestBody Student student) {
        boolean rs = studentService.update(student);
        if (rs) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 删除学生
     *
     * @param student 学生ID
     * @return 删除结果的响应实体
     */
    @PostMapping("/delete")
    public ResponseEntity<?> delete(@RequestBody Student student) {
        boolean rs = studentService.delete(student.getStudentId());
        if (rs) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 学生注册
     *
     * @param student 学生对象
     * @return 注册结果的响应实体
     */
    @PostMapping("/add")
    public ResponseEntity<?> register(@RequestBody Student student) {
        if (studentService.selectById(student.getStudentId()) != null) {
            return ResponseEntity.badRequest().build();
        }
        boolean rs = studentService.add(student);
        if (rs) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.badRequest().build();
        }
    }
}
